package algorithmic_data_structure.堆与堆排序.mycode;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * 堆排序
 */
public class HeapSort {
    
    public static void main(String[] args) {
        int[] array = {
                1,2,3,4,2,1,5,4,3,6,5,2,7,8,9,4,7,8
        };
        heapSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ,");
        }
    }

    public static void heapSort(int[] array)
    {
        if (array == null || array.length < 2)
        {
            return;
        }

        PriorityQueue<Integer> minHeap = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //小根堆
                return o1 - o2;
            }
        });

        for (int i = 0; i < array.length; i++) {
            minHeap.add(array[i]);
        }

        for (int i = 0; i < array.length; i++) {
            array[i] = minHeap.poll();
        }
    }

}
